﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArraysAndStrings.PivotIndex
{
    class MainProject
    {
        static void Main(string[] args)
        {
            int[] nums = new int[] { 2,1,-1 };
            Console.WriteLine(PivotIndex(nums));
            Console.ReadKey();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="nums"></param>
        /// <returns></returns>
        public static int PivotIndex(int[] nums)
        {
            if (nums.Length<=1)
            {
                return 0;
            }
            int allSum = 0;
            foreach (var item in nums)
            {
                allSum += item;
            }
            int leftAllSun = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                if (leftAllSun*2+ nums[i] == allSum)
                {
                    return i;
                }
                else
                {
                    leftAllSun += nums[i];
                }
            }
            return -1;
        }

      
    }
}
